package com.jbp.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jbp.dao.PoolDao;
import com.jbp.domin.LuckReq2;
import com.jbp.exception.RRException;
import com.jbp.model.Pool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jbp.utils.PageUtils;
import com.jbp.utils.Query;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Service
public class PoolService extends ServiceImpl<PoolDao, Pool> implements IService<Pool> {

    @Autowired
    private UserDailyLuckRecordService userDailyLuckRecordService;
    @Autowired
    private PoolXUserService poolXUserService;

    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<Pool> queryWrapper = new QueryWrapper<>();
        if (params.get("id") != null) {
            queryWrapper.like("id",params.get("id"));
        }
        if (params.get("name") != null) {
            queryWrapper.like("name",params.get("name"));
        }
        if (params.get("price") != null) {
            queryWrapper.like("price",params.get("price"));
        }
        if (params.get("start") != null) {
            queryWrapper.like("start",params.get("start"));
        }
        if (params.get("state") != null) {
            queryWrapper.like("state",params.get("state"));
        }
        if (params.get("inx") != null) {
            queryWrapper.like("inx",params.get("inx"));
        }
        if (params.get("com") != null) {
            queryWrapper.like("com",params.get("com"));
        }
        if (params.get("type") != null) {
            queryWrapper.like("type",params.get("type"));
        }
        if (params.get("fromDate")!=null) {
            queryWrapper.ge("create_time", params.get("fromDate"));
        }
        if (params.get("toDate")!=null) {
            queryWrapper.le("DATE_FORMAT(create_time,   '%Y-%m-%d')", params.get("toDate"));
        }
        queryWrapper.orderByDesc("id");
        IPage<Pool> page = this.page(
            new Query<Pool>().getPage(params), queryWrapper
        );
        return new PageUtils(page);
    }

    public Pool getPool(LuckReq2 luckReq) {
        // 获取用户在该奖池的总投入
        BigDecimal pay = userDailyLuckRecordService.getAllPayByType(luckReq.getUserId(), luckReq.getPrice());
        // 获取门槛大于用户投入的第一个奖池
        QueryWrapper<Pool> wrapper = new QueryWrapper<>();
        wrapper.notLike("name", "设备A池");
        wrapper.notLike("name", "设备B池");
        wrapper.eq("type", 0);
        wrapper.eq("state", 1);
        wrapper.eq("price", luckReq.getPrice());
        wrapper.le("start", pay);
        wrapper.orderByDesc("inx");
        wrapper.last("limit 1");
        Pool pool = getOne(wrapper);

        if (pool == null) {
            throw new RRException("奖池错误");
        }
        return pool;
    }


    /**
     * 获取设备池A或B
     * @param luckReq
     * @return
     */
    public Pool getDevicePool(LuckReq2 luckReq) {
        // 获取用户在该奖池的总投入
        BigDecimal pay = userDailyLuckRecordService.getAllPayByType(luckReq.getUserId(), luckReq.getPrice());
        // 获取门槛大于用户投入的第一个奖池
        QueryWrapper<Pool> wrapper = new QueryWrapper<>();
        wrapper.like("name", luckReq.getPoolName());
        wrapper.eq("type", 0);
        wrapper.eq("state", 1);
        wrapper.eq("price", luckReq.getPrice());
        wrapper.le("start", pay);
        wrapper.orderByDesc("inx");
        wrapper.last("limit 1");
        Pool pool = getOne(wrapper);

        if (pool == null) {
            throw new RRException("奖池错误");
        }
        return pool;
    }

    //取机器人奖池
    public Pool getRobPool(LuckReq2 luckReq) {
        // 获取门槛大于用户投入的第一个奖池
        QueryWrapper<Pool> wrapper = new QueryWrapper<>();
        //排除设备池
//        wrapper.notLike("name", "设备A池");
//        wrapper.notLike("name", "设备B池");
        wrapper.like("name", "1号公池");
        wrapper.eq("type", 0);
        wrapper.eq("state", 1);
        wrapper.eq("price", luckReq.getPrice());
//        wrapper.le("start", 0);
//        wrapper.orderByDesc("inx");
        wrapper.last("limit 1");
        Pool pool = getOne(wrapper);

        if (pool == null) {
            throw new RRException("奖池错误");
        }
        return pool;
    }

    public List<Pool> getByPrice(int price) {
        QueryWrapper<Pool> wrapper = new QueryWrapper<>();
        wrapper.eq("price", price);
        return list(wrapper);
    }
}
